package com.xiaoshuidi.cloud.module.rooms.framework.config;

import cn.hutool.extra.spring.SpringUtil;
import com.xiaoshuidi.cloud.framework.datapermission.core.rule.dept.DeptDataPermissionRuleCustomizer;
import com.xiaoshuidi.cloud.module.rooms.framework.rule.EsDeptDataPermissionRule;
import com.xiaoshuidi.cloud.module.rooms.pojo.*;
import com.xiaoshuidi.cloud.module.system.api.permission.PermissionApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class DataPermissionConfiguration {

    @Bean
    public DeptDataPermissionRuleCustomizer sysDeptDataPermissionRuleCustomizer() {
        return rule -> {
            // dept 基于部门的数据权限
            rule.addDeptColumn(Apartment.class, "dept_id"); // WHERE id = ?
            rule.addDeptColumn(RoomHouse.class, "dept_id");
            rule.addDeptColumn(RoomLayout.class, "dept_id");
            rule.addDeptColumn(RoomHouseReserve.class, "dept_id");
            rule.addDeptColumn(RoomHouseEstimate.class, "dept_id");
            rule.addDeptColumn(EquipmentStocktaking.class, "dept_id");
            rule.addDeptColumn(RoomEquipment.class, "dept_id");
        };
    }

    @Bean
    public EsDeptDataPermissionRule esDeptDataPermissionRule(PermissionApi permissionApi) {
        try {
            PermissionApi permissionApiImpl = SpringUtil.getBean("permissionApiImpl", PermissionApi.class);
            if (permissionApiImpl != null) {
                permissionApi = permissionApiImpl;
            }
        } catch (Exception ignored) {
        }
        // 创建 DeptDataPermissionRule 对象
        EsDeptDataPermissionRule rule = new EsDeptDataPermissionRule(permissionApi);
        return rule;
    }

}
